
package com.tspp.entrantsAcceptor.servlets;

import com.tspp.dao.DaoException;
import com.tspp.dao.dto.Entrant;
import com.tspp.entrantsAcceptor.EntrantsAcceptorBean;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Maks
 */
public class UpdateAddEntrantServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String surname = request.getParameter("surname");
        String address = request.getParameter("address");
        String typeOfOperation = request.getParameter("id");
        String gr = request.getParameter("groupa");
        System.out.println(name);
        System.out.println(surname);
        String grCode = null;
        try {
            EntrantsAcceptorBean bean = new EntrantsAcceptorBean();
            grCode = bean.getGroupCode(Integer.parseInt(gr));
            if (name.equals("") || surname.equals("") || address.equals("")) {
                System.out.println(URLEncoder.encode(surname, "UTF-8"));
                String url = response.encodeRedirectURL("update.jsp?error=" + "Fields can not be empty&name=" + encode(name)
                        + "&surname=" + encode(surname) + "&address=" + encode(address) + "&id=" + typeOfOperation + "&group=" + encode(grCode));
                response.sendRedirect(url);
                return;
            }
            Entrant en = new Entrant();
            en.setName(name);
            en.setSurname(surname);
            en.setAddress(address);
            en.setIdGroup(Integer.parseInt(gr));
            if (typeOfOperation.equals("add")) {
                bean.addEntrant(en);
            } else {
                int id = Integer.parseInt(typeOfOperation);
                bean.updateEntrant(id, en);
            }
            response.sendRedirect("index.jsp");
        } catch (DaoException ex) {
            if (ex.getMessage().contains(DaoException.MAX_ENTRANTS_IN_GROUP)) {
                String url = response.encodeRedirectURL("update.jsp?error=" + " Max number of entrants reached for this group&name=" + encode(name)
                        + "&surname=" + encode(surname) + "&address=" + encode(address) + "&id=" + typeOfOperation + "&group=" + encode(grCode));
                response.sendRedirect(url);
                return;
            } else {
                throw new ServletException(ex.getMessage());
                //Logger.getLogger(UpdateAddEntrantServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            //Logger.getLogger(UpdateAddEntrantServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private String encode(String par) throws UnsupportedEncodingException {
        return URLEncoder.encode(par, "UTF-8");
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
